#ifndef ANDROID_HEADER_H
#define ANDROID_HEADER_H
#include <jni.h>
#include <string>

#include <QString>
#include "../common/tooldb.h"


extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_openDB(JNIEnv* env,jobject thiz,jstring fileName)
{
    QString result = "";
    const char* fileNameChar = env->GetStringUTFChars(fileName, NULL);

    if (fileNameChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::openDB(fileNameChar);
    }
    return env->NewStringUTF(result.toUtf8().constData());

}

extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_selectDataMultiple(JNIEnv* env,jobject thiz,jstring sql)
{
    QString result = "";
    const char* sqlChar = env->GetStringUTFChars(sql, NULL);

    if (sqlChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::selectDataMultiple(sqlChar);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}

extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_selectDataSingle(JNIEnv* env,jobject thiz,jstring sql)
{
    QString result = "";
    const char* sqlChar = env->GetStringUTFChars(sql, NULL);

    if (sqlChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::selectDataSingle(sqlChar);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}

extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_addData(JNIEnv* env,jobject thiz,jstring sql)
{
    QString result = "";
    const char* sqlChar = env->GetStringUTFChars(sql, NULL);

    if (sqlChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::addData(sqlChar);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}

extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_deleteData(JNIEnv* env,jobject thiz,jstring sql)
{
    QString result = "";
    const char* sqlChar = env->GetStringUTFChars(sql, NULL);

    if (sqlChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::deleteData(sqlChar);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}

extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_updateData(JNIEnv* env,jobject thiz,jstring sql)
{
    QString result = "";
    const char* sqlChar = env->GetStringUTFChars(sql, NULL);

    if (sqlChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::updateData(sqlChar);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}

extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_sendGet(JNIEnv* env,jobject thiz,jstring url,jstring headerJson,jint timeout)
{
    QString result = "";
    const char* urlChar = env->GetStringUTFChars(url, NULL);

    const char* headerJsonChar = env->GetStringUTFChars(headerJson, NULL);
    if (urlChar == NULL || headerJsonChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::sendGet(urlChar,headerJsonChar,timeout);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}
/*
    static QString sendPost(const QString& url,const QString& headerJson,QString paramJson,int timeout);
*/
extern "C" JNIEXPORT jstring JNICALL Java_com_tool_db_ToolUtils_sendPost(JNIEnv* env,jobject thiz,jstring url,jstring headerJson,jstring paramJson,jint timeout)
{
    QString result = "";
    const char* urlChar = env->GetStringUTFChars(url, NULL);

    const char* headerJsonChar = env->GetStringUTFChars(headerJson, NULL);

    const char* paramJsonChar = env->GetStringUTFChars(paramJson,NULL);
    if (urlChar == NULL || headerJsonChar == NULL || paramJsonChar == NULL) {

        result = JsonTool::failed(-5,"Parameter Conversion Failed");

    }else{
        result = ToolDB::sendPost(urlChar,headerJsonChar,paramJsonChar,timeout);
    }
    return env->NewStringUTF(result.toUtf8().constData());
}
#endif // ANDROID_HEADER_H
